package com.amkai.aes.webservice.binary.objecthandler;

import com.amkai.aes.aescommon.service.binary.job.registration.RegistrationValidationJob;
import com.amkai.aes.aescommon.service.result.registration.RegistrationValidationResult;
import com.amkai.aes.generatedmodels.registration.billinggroup.BillinggroupDomain;
import com.amkai.aes.generatedmodels.registration.caseforms.PmCaseDomain;
import com.amkai.aes.generatedmodels.registration.main.MainDomain;
import com.amkai.aes.server.validation.providers.BillinggroupDomainRelatedChangesValidationProvider;
import com.amkai.aes.server.validation.providers.MainDomainDeleteValidationProvider;
import com.amkai.aes.server.validation.providers.PmCaseDomainRelatedChangesValidationProvider;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.rad.designerbase.domain.Domain;

// KKORNEL: AES-3865 - Old name was DeleteValidationHandler
public class RegistrationValidationHandler implements JobObjectHandler<RegistrationValidationJob, Domain, RegistrationValidationResult> {

	public RegistrationValidationResult process(RegistrationValidationJob job, Domain domain) throws Exception {
		RegistrationValidationResult result = new RegistrationValidationResult();
		if (job.getJobKind() != null) {
			switch (job.getJobKind()) {
				case DELETE_BILLINGGROUP : {
					processDeleteFromMainDomainJobKinds(job, domain, result);
					break;
				}
				
				case DELETE_PMCASE : {
					processDeleteFromMainDomainJobKinds(job, domain, result);
					break;
				}
				
				case DELETE_INSURANCEPOLICY : {
					processDeleteFromMainDomainJobKinds(job, domain, result);
					break;
				}
				
				case DELETE_BILLINGGROUPINSURANCE : {
					processBillingGroupDomainRelatedChangesJobKinds(job, domain, result);
					break;
				}

				case CHANGE_BILLINGGROUPINSURANCE_ORDER : {
					processBillingGroupDomainRelatedChangesJobKinds(job, domain, result);
					break;
				}
				// KKORNEL: AES-8219
				case DELETE_BILLINGGROUP_FROM_PMCASE: {
					processPmCaseDomainRelatedChangesJobKinds(job, domain, result);
				}
			}
		}
		return result;
	}
	
	private void processDeleteFromMainDomainJobKinds(RegistrationValidationJob job, Domain domain, RegistrationValidationResult result) {
		if (domain instanceof MainDomain) {
			result.setErrorMessage(new MainDomainDeleteValidationProvider().isValid(job, domain));
		}		
	}

	private void processBillingGroupDomainRelatedChangesJobKinds(RegistrationValidationJob job, Domain domain, RegistrationValidationResult result) {
		if (domain instanceof BillinggroupDomain){
			result.setErrorMessage(new BillinggroupDomainRelatedChangesValidationProvider().isValid(job, domain));			
		}
	}

	// KKORNEL: AES-8219
	private void processPmCaseDomainRelatedChangesJobKinds(RegistrationValidationJob job, Domain domain, RegistrationValidationResult result) {
		if (domain instanceof PmCaseDomain) {
			result.setErrorMessage(new PmCaseDomainRelatedChangesValidationProvider().isValid(job, (PmCaseDomain)domain));			
		}
	}
}
